<template>
  <common-page :title="isEdit ? '编辑告警规则' : '新增告警规则'">
    <div slot="header">
      <el-button @click="$emit('close')">返回</el-button>
      <el-button type="primary" @click="submit" :loading="loading"
        >保存
      </el-button>
    </div>
    <div slot="content" class="common-page-content">
      <div class="content-wrap">
        <el-form
          :model="form"
          ref="form"
          :inline="true"
          label-width="110px"
          :rules="rules"
          @submit.native.prevent
        >
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item
                label="告警规则类别"
                prop="ruleType"
                class="ruleItem"
              >
                <el-select
                  placeholder="请选择"
                  v-model="form.ruleType"
                  @change="changeRuleType"
                >
                  <el-option
                    v-for="(item, index) in ruleTypes"
                    :key="index"
                    :label="item.paramName"
                    :value="item.paramValue"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="规则名称" prop="ruleName" class="ruleItem">
                <common-input
                  clearable
                  v-model="form.ruleName"
                  :maxlength="16"
                  data-test="alarm_rule-input-ruleName"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="规则编码" prop="ruleCode" class="ruleItem">
                <common-input
                  clearable
                  v-model="form.ruleCode"
                  :maxlength="64"
                  data-test="alarm_rule-input-ruleCode"
                  disabled
                />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="告警码" prop="alarmCode" class="ruleItem">
                <common-input
                  clearable
                  v-model="form.alarmCode"
                  readonly
                  @focus="toAlarmList"
                  data-test="alarm_rule-input-alarmCode"
                >
                  <span slot="suffix" style="margin-right: 8px">
                    <commonIcon
                      iconName="icon-icon__xuanzexialacaidan-copy"
                      iconType="icon"
                      :iconSize="16"
                      isStyle
                      @click="toAlarmList"
                    ></commonIcon>
                  </span>
                </common-input>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="告警类型" prop="alarmType" class="ruleItem">
                <el-select
                  placeholder="请选择"
                  v-model="form.alarmType"
                  disabled
                >
                </el-select>
              </el-form-item>
            </el-col>
            <!-- <el-col :span="12">
              <el-form-item
                label="告警子类型"
                prop="alarmBusiness"
                class="ruleItem"
              >
                <el-select
                  placeholder="请选择"
                  v-model="form.alarmBusiness"
                  disabled
                >
                </el-select>
              </el-form-item>
            </el-col> -->

            <el-col :span="12">
              <el-form-item label="告警等级" prop="degree" class="ruleItem">
                <el-select placeholder="请选择" v-model="form.degree" disabled>
                  <el-option
                    v-for="(item, index) in alarmLevelList"
                    :key="index"
                    :label="item.name"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item
                label="级别提升基数"
                prop="alertDegreeUp"
                class="ruleItem"
              >
                <common-input
                  v-model="form.alertDegreeUp"
                  type="number"
                  :maxlength="64"
                />
              </el-form-item>
            </el-col>

            <!-- <el-col :span="12">
              <el-form-item label="规则类型" prop="alertType" class="ruleItem">
                <el-select placeholder="请选择" v-model="form.alertType">
                  <el-option
                    v-for="(item, index) in alertTypeList"
                    :key="index"
                    :label="item.paramName"
                    :value="item.paramValue"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col> -->
            <!--
            <el-col :span="12">
              <el-form-item label="杀伤链" prop="killChain" class="ruleItem">
                <el-select placeholder="请选择" v-model="form.killChain">
                  <el-option
                    v-for="(item, index) in killChainList"
                    :key="index"
                    :label="item.paramName"
                    :value="item.paramValue"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col> -->

            <el-col
              :span="12"
              v-if="
                form.ruleType !== 'multiSequenceAlert' &&
                form.ruleType !== 'analyseAlert'
              "
            >
              <el-form-item
                label="索引类型"
                prop="ruleDetails.dataSourceId"
                class="ruleItem"
              >
                <el-select
                  placeholder="请选择"
                  v-model="form.ruleDetails.dataSourceId"
                  @change="changeIndexType"
                >
                  <el-option
                    v-for="(item, index) in indexTypes"
                    :key="index"
                    :label="item.paramName"
                    :value="item.paramValue"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col
              :span="12"
              v-if="
                form.ruleType !== 'multiSequenceAlert' &&
                form.ruleType !== 'analyseAlert'
              "
            >
              <el-form-item
                label="告警合并字段"
                prop="mergeField"
                class="ruleItem"
              >
                <el-select
                  v-model="form.ruleDetails.mergeField"
                  multiple
                  filterable
                  placeholder="请选择"
                >
                  <el-option
                    v-for="(item, index) in typeStaticList"
                    :key="index"
                    :label="item.columnAlias"
                    :value="item.columnName"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col
              :span="12"
              v-if="
                form.ruleType !== 'singleAlert' &&
                form.ruleType !== 'analyseAlert'
              "
            >
              <el-form-item
                label="监控频率"
                prop="ruleDetails.frequency"
                class="ruleItem2"
              >
                <common-input
                  type="number"
                  v-model="form.ruleDetails.frequency"
                  :maxlength="64"
                />
              </el-form-item>

              <el-form-item prop="ruleDetails.frequencyType" class="ruleItem3">
                <el-select
                  v-model="form.ruleDetails.frequencyType"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in unitOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col
              :span="12"
              v-if="
                form.ruleType !== 'singleAlert' &&
                form.ruleType !== 'analyseAlert'
              "
            >
              <el-form-item
                label="统计时间范围"
                prop="ruleDetails.timesInterval"
                class="ruleItem2"
              >
                <common-input
                  type="number"
                  :maxlength="64"
                  v-model="form.ruleDetails.timesInterval"
                />
              </el-form-item>

              <el-form-item prop="intervalType" class="ruleItem3">
                <el-select
                  v-model="form.ruleDetails.intervalType"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in unitOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <template
              v-if="
                form.ruleDetails.alertConditions &&
                form.ruleDetails.alertConditions[0]
              "
            >
              <el-col :span="24" v-if="form.ruleType === 'multiSequenceAlert'">
                <el-form-item
                  label="索引类型1"
                  class="ruleItem"
                  prop="ruleDetails.alertConditions[0].dataSourceId"
                >
                  <el-select
                    v-model="form.ruleDetails.alertConditions[0].dataSourceId"
                    placeholder="请选择"
                    @change="changeTypeIndex1"
                  >
                    <el-option
                      v-for="(item2, index2) in indexTypes"
                      :key="index2"
                      :label="item2.paramName"
                      :value="item2.paramValue"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>

              <el-col :span="24" v-if="form.ruleType === 'multiSequenceAlert'">
                <el-form-item
                  label="关联字段1"
                  v-for="(item, index) in form.ruleDetails.alertConditions[0]
                    .associationField"
                  :key="index"
                  class="ruleItem6"
                  style="margin-bottom: 0"
                  required
                >
                  <div style="display: flex">
                    <el-form-item
                      :prop="
                        'ruleDetails.alertConditions[0].associationField.' +
                        index
                      "
                      class="ruleItem6"
                    >
                      <el-select
                        v-model="
                          form.ruleDetails.alertConditions[0].associationField[
                            index
                          ]
                        "
                        filterable
                        placeholder="请选择"
                      >
                        <el-option
                          v-for="(item2, index2) in relationOption1"
                          :key="index2"
                          :label="item2.columnAlias"
                          :value="item2.columnName"
                        >
                        </el-option>
                      </el-select>
                    </el-form-item>

                    <el-button
                      v-if="isShowDeleteAssociatedField1"
                      style="margin-left: 6px; height: 34px"
                      plain
                      type="danger"
                      @click.prevent="deleteAssociatedField1(item, index)"
                      >删除
                    </el-button>

                    <el-button
                      :class="{ disabledAdd: index !== 0 }"
                      style="margin-left: 6px; height: 34px"
                      type="primary"
                      @click="addAssociatedField1"
                      plain
                      >新增
                    </el-button>
                  </div>
                </el-form-item>
              </el-col>

              <el-col :span="24" v-if="form.ruleType === 'multiSequenceAlert'">
                <el-form-item
                  label="过滤规则1"
                  prop="ruleDetails.alertConditions[0].conditionStr"
                  class="ruleItem"
                >
                  <common-input
                    type="textarea"
                    style="width: 100%"
                    :verify="false"
                    readonly
                    v-model="form.ruleDetails.alertConditions[0].conditionStr"
                  >
                  </common-input>
                  <div
                    @click="toConfigure(1)"
                    class="rule-configure"
                    :style="{ '--theme-color': systemInfo.theme }"
                  >
                    配置
                  </div>
                </el-form-item>
              </el-col>
              <el-col :span="24" v-if="form.ruleType === 'multiSequenceAlert'">
                <div class="split-line"></div>
              </el-col>

              <el-col :span="24" v-if="form.ruleType === 'multiSequenceAlert'">
                <el-form-item
                  label="索引类型2"
                  class="ruleItem"
                  prop="ruleDetails.alertConditions[1].dataSourceId"
                >
                  <el-select
                    v-model="form.ruleDetails.alertConditions[1].dataSourceId"
                    placeholder="请选择"
                    @change="changeTypeIndex2"
                  >
                    <el-option
                      v-for="(item2, index2) in indexTypes"
                      :key="index2"
                      :label="item2.paramName"
                      :value="item2.paramValue"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>

              <el-col :span="24" v-if="form.ruleType === 'multiSequenceAlert'">
                <el-form-item
                  label="关联字段2"
                  v-for="(item, index) in form.ruleDetails.alertConditions[1]
                    .associationField"
                  :key="index"
                  class="ruleItem6"
                  style="margin-bottom: 0"
                  required
                >
                  <div style="display: flex">
                    <el-form-item
                      :prop="
                        'ruleDetails.alertConditions[1].associationField.' +
                        index
                      "
                      class="ruleItem6"
                    >
                      <el-select
                        v-model="
                          form.ruleDetails.alertConditions[1].associationField[
                            index
                          ]
                        "
                        filterable
                        placeholder="请选择"
                      >
                        <el-option
                          v-for="(item2, index2) in relationOption2"
                          :key="index2"
                          :label="item2.columnAlias"
                          :value="item2.columnName"
                        >
                        </el-option>
                      </el-select>
                    </el-form-item>

                    <el-button
                      v-if="isShowDeleteAssociatedField2"
                      style="margin-left: 6px; height: 34px"
                      plain
                      type="danger"
                      @click.prevent="deleteAssociatedField2(item, index)"
                      >删除
                    </el-button>
                    <el-button
                      :class="{ disabledAdd: index !== 0 }"
                      style="margin-left: 6px; height: 34px"
                      type="primary"
                      @click="addAssociatedField2"
                      plain
                      >新增
                    </el-button>
                  </div>
                </el-form-item>
              </el-col>

              <el-col :span="24" v-if="form.ruleType === 'multiSequenceAlert'">
                <el-form-item
                  label="过滤规则2"
                  prop="ruleDetails.alertConditions[1].conditionStr"
                  class="ruleItem"
                >
                  <common-input
                    type="textarea"
                    style="width: 100%"
                    :verify="false"
                    readonly
                    v-model="form.ruleDetails.alertConditions[1].conditionStr"
                  >
                  </common-input>
                  <div
                    @click="toConfigure(2)"
                    class="rule-configure"
                    :style="{ '--theme-color': systemInfo.theme }"
                  >
                    配置
                  </div>
                </el-form-item>
              </el-col>
            </template>

            <el-col :span="24" v-if="form.ruleType === 'singleSequenceAlert'">
              <el-form-item
                class="ruleItem"
                style="margin-bottom: 0"
                v-for="(item, index) in form.ruleDetails.referenceFields"
                :key="index"
                :required="index === 0 ? true : false"
              >
                <span slot="label">
                  <span>{{ index === 0 ? "时序字段值" : "" }}</span>
                </span>
                <div style="display: flex">
                  <el-form-item
                    :prop="'ruleDetails.referenceFields.' + index + '.name'"
                    class="ruleItem4"
                  >
                    <el-select
                      v-model="form.ruleDetails.referenceFields[index].name"
                      :style="{ display: index === 0 ? 'block' : 'none' }"
                      placeholder="请选择"
                      filterable
                    >
                      <el-option
                        v-for="(item2, index2) in typeSearchList"
                        :key="index2"
                        :label="item2.columnAlias"
                        :value="item2.columnName"
                      >
                      </el-option>
                    </el-select>
                  </el-form-item>

                  <el-form-item
                    :prop="'ruleDetails.referenceFields.' + index + '.value'"
                    class="ruleItem5"
                  >
                    <common-input
                      clearable
                      v-model="form.ruleDetails.referenceFields[index].value"
                      :maxlength="64"
                      style="margin-left: 10px"
                    />
                  </el-form-item>
                  <el-button
                    v-if="isShowDeleteTimeField"
                    style="margin-left: 6px; height: 34px"
                    plain
                    type="danger"
                    @click.prevent="deleteTimeField(item, index)"
                    >删除
                  </el-button>
                  <el-button
                    :class="{ disabledAdd: index !== 0 }"
                    style="margin-left: 6px; height: 34px"
                    type="primary"
                    @click="addTimeField"
                    :disabled="
                      form.ruleDetails.referenceFields.length >= 10
                        ? true
                        : false
                    "
                    plain
                    >新增
                  </el-button>
                </div>
              </el-form-item>
            </el-col>

            <el-col :span="12" v-if="form.ruleType === 'limitAlert'">
              <el-form-item
                label="对比时间范围"
                prop="ruleDetails.limitInterval"
                class="ruleItem"
              >
                <common-input
                  type="number"
                  v-model="form.ruleDetails.limitInterval"
                  :maxlength="64"
                >
                  <span slot="prepend">前</span>
                  <span slot="append">天</span>
                </common-input>
              </el-form-item>
            </el-col>

            <el-col :span="12" v-if="form.ruleType === 'multiAlert'">
              <el-form-item
                label="日志条数"
                type="number"
                prop="ruleDetails.logNum"
                class="ruleItem"
              >
                <common-input
                  type="number"
                  v-model="form.ruleDetails.logNum"
                  :maxlength="64"
                />
              </el-form-item>
            </el-col>

            <el-col :span="12" v-if="form.ruleType === 'multiAlert'">
              <el-form-item
                label="不同维度"
                prop="ruleDetails.dimensionDiff"
                class="ruleItem"
              >
                <el-select
                  v-model="form.ruleDetails.dimensionDiff"
                  placeholder="请选择"
                  filterable
                >
                  <el-option
                    v-for="(item, index) in typeSearchList"
                    :key="index"
                    :label="item.columnAlias"
                    :value="item.columnName"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col
              :span="12"
              v-if="
                form.ruleType === 'countAlert' || form.ruleType === 'limitAlert'
              "
            >
              <el-form-item
                label="统计方式"
                prop="ruleDetails.statsType"
                class="ruleItem"
              >
                <el-select
                  placeholder="请选择"
                  v-model="form.ruleDetails.statsType"
                >
                  <el-option
                    v-for="item in form.ruleType === 'countAlert'
                      ? statsTypeList
                      : statsTypeList2"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col
              :span="12"
              v-if="
                form.ruleType === 'countAlert' &&
                form.ruleDetails.statsType !== 'count'
              "
            >
              <el-form-item
                label="指标字段"
                prop="ruleDetails.aggField"
                class="ruleItem"
              >
                <el-select
                  placeholder="请选择"
                  filterable
                  v-model="form.ruleDetails.aggField"
                >
                  <template v-for="(item, index) in typeShowLessList">
                    <el-option
                      v-if="
                        item.columnType === 'Integer' ||
                        item.columnType === 'Double' ||
                        item.columnType === 'float' ||
                        item.columnType === 'Long'
                      "
                      :key="index"
                      :label="item.columnAlias"
                      :value="item.columnName"
                    >
                    </el-option>
                  </template>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="12" v-if="form.ruleType === 'limitAlert'">
              <el-form-item
                label="对比字段"
                prop="ruleDetails.limitField"
                class="ruleItem"
              >
                <el-select
                  placeholder="请选择"
                  filterable
                  v-model="form.ruleDetails.limitField"
                >
                  <template v-for="(item, index) in typeShowList">
                    <el-option
                      v-if="
                        item.columnType === 'Long' ||
                        item.columnType === 'Integer'
                      "
                      :key="index"
                      :label="item.columnAlias"
                      :value="item.columnName"
                    >
                    </el-option>
                  </template>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="12" v-if="form.ruleType === 'limitAlert'">
              <el-form-item
                label="条件1"
                prop="ruleDetails.rule1Type"
                class="ruleItem2"
              >
                <el-select
                  v-model="form.ruleDetails.rule1Type"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="(item, index) in conditionOperList"
                    :key="index"
                    :label="item.paramName"
                    :value="item.paramValue"
                  >
                  </el-option>
                </el-select>
              </el-form-item>

              <el-form-item prop="ruleDetails.rule1Value" class="ruleItem3">
                <common-input
                  type="number"
                  v-model="form.ruleDetails.rule1Value"
                  :clearable="false"
                  :maxlength="64"
                >
                  <span slot="append">%</span>
                </common-input>
              </el-form-item>
            </el-col>

            <el-col :span="12" v-if="form.ruleType === 'limitAlert'">
              <el-form-item
                label="条件2"
                prop="ruleDetails.rule2Type"
                class="ruleItem2"
              >
                <el-select
                  v-model="form.ruleDetails.rule2Type"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="(item, index) in conditionOperList"
                    :key="index"
                    :label="item.paramName"
                    :value="item.paramValue"
                  >
                  </el-option>
                </el-select>
              </el-form-item>

              <el-form-item prop="ruleDetails.rule2Value" class="ruleItem3">
                <common-input
                  type="number"
                  v-model="form.ruleDetails.rule2Value"
                  :maxlength="64"
                >
                  <span slot="append">%</span>
                </common-input>
              </el-form-item>
            </el-col>

            <el-col :span="12" v-if="form.ruleType === 'countAlert'">
              <el-form-item
                label="阈值"
                prop="ruleDetails.threshold"
                class="ruleItem"
              >
                <common-input
                  type="number"
                  v-model="form.ruleDetails.threshold"
                  :maxlength="64"
                />
              </el-form-item>
            </el-col>

            <el-col
              :span="24"
              v-if="
                form.ruleType !== 'multiSequenceAlert' &&
                form.ruleType !== 'analyseAlert'
              "
            >
              <el-form-item
                label="过滤规则"
                prop="ruleDetails.conditionStr"
                class="ruleItem"
              >
                <common-input
                  type="textarea"
                  style="width: 100%"
                  :verify="false"
                  readonly
                  v-model="form.ruleDetails.conditionStr"
                >
                </common-input>
                <div
                  @click="toConfigure"
                  class="rule-configure"
                  :style="{ '--theme-color': systemInfo.theme }"
                >
                  配置
                </div>
              </el-form-item>
            </el-col>

            <el-col :span="24" v-if="form.ruleType !== 'analyseAlert'">
              <el-form-item label="备注" prop="remark" class="ruleItem">
                <common-input
                  type="textarea"
                  clearable
                  style="width: 100%"
                  v-model="form.remark"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>

    <AlarmCodeSelect
      ref="alarmCode"
      v-if="isShowAlarmSelect"
      @close="isShowAlarmSelect = false"
      @getAlarmCode="getAlarmCode"
    />

    <Configure ref="configure" :title="'配置'" @getCondition="getCondition" />
  </common-page>
</template>

<script>
import {
  ruleEdit,
  ruleAdd,
  ruleDetail,
  addAlarmInfo,
  alarmCodeList,
  autoRuleCode,
} from "@/api/datascan/alarmRules/rules";

import { UNITOPTIONS, STATISTICALS, CONDITIONOPERATOR } from "@/utils/constant";
import AlarmCodeSelect from "../AlarmCodeSelect.vue";
import Configure from "../Configure.vue";
import { mapGetters, mapState } from "vuex";
import { generateSqlObj, generateSqlString, generateTuple } from "@/utils/util";
import { getAlarmRulesParams } from "@/utils/getParams";
import commonIcon from "@/components/CommonIcon";

export default {
  name: "RuleAdd",
  components: { AlarmCodeSelect, Configure, commonIcon },
  props: {
    isEdit: {
      type: Boolean,
      default: false,
    },
    editForm: {
      type: Object,
      default: () => {},
    },
    currentNodeKey: {
      type: String,
      default: "",
    },
    typeStaticOption: {
      type: Array,
      default: () => [],
    },
    typeStaticListOption: {
      type: Array,
      default: () => [],
    },
  },
  computed: {
    ...mapGetters(["systemInfo", "isAdmin"]),
    ...mapState("rule", [
      "ruleTypes",
      "alarmLevelList",
      "alertTypeList",
      "killChainList",
      "indexTypes",
      "conditionOperList",
      "alarmTypeList",
      "alarmLevelList",
      "typeSearchList",
      "typeStaticList",
      "typeShowList",
      "typeShowLessList",
    ]),
    ...mapState("log", ["operatorList"]),
  },
  watch: {
    "form.ruleType": {
      handler: function (val) {
        if (val === "countAlert" && !this.form.ruleDetails.statsType) {
          this.$set(this.form.ruleDetails, "statsType", "count");
        } else if (
          val === "limitAlert" &&
          (!this.form.ruleDetails.statsType ||
            this.form.ruleDetails.statsType === "count")
        ) {
          this.$set(this.form.ruleDetails, "statsType", "avg");
        }
      },
      deep: true,
      immediate: true,
    },
  },
  created() {
    if (this.currentNodeKey) {
      this.form.killChain = this.currentNodeKey;
    }

    if (this.isEdit) {
      //编辑
      this.form = this.editForm;
      this.form.ruleDetails = this.form.ruleDetails
        ? JSON.parse(this.form.ruleDetails)
        : {};

      // 告警类型为多数据源时序告警时，根据不同索引类型对关联字段下拉数据进行分开处理
      // 告警类型为其他时，直接根据索引类型值对告警合并字段下拉数据进行处理
      if (this.form.ruleType === "multiSequenceAlert") {
        let option1 =
          this.form.ruleDetails.alertConditions[0].dataSourceId ===
          "index-bwda-ark-apisecurity"
            ? this.typeStaticOption
            : this.typeStaticListOption;

        this.relationOption1 = option1.filter((item) => item.isSearch === 0);

        let option2 =
          this.form.ruleDetails.alertConditions[1].dataSourceId ===
          "index-bwda-ark-apisecurity"
            ? this.typeStaticOption
            : this.typeStaticListOption;
        this.relationOption2 = option2.filter((item) => item.isSearch === 0);
      } else {
        this.optionList =
          this.form.ruleDetails.dataSourceId &&
          this.form.ruleDetails.dataSourceId === "index-bwda-ark-apisecurity"
            ? this.typeStaticOption
            : this.typeStaticListOption;

        this.changeData();
      }

      this.$set(
        this.form.ruleDetails,
        "frequency",
        this.form.ruleDetails.frequency || "1"
      );
      this.$set(
        this.form.ruleDetails,
        "frequencyType",
        this.form.ruleDetails.frequencyType || "min"
      );

      this.$set(
        this.form.ruleDetails,
        "timesInterval",
        this.form.ruleDetails.timesInterval || "1"
      );
      this.$set(
        this.form.ruleDetails,
        "intervalType",
        this.form.ruleDetails.intervalType || "min"
      );

      this.$set(
        this.form.ruleDetails,
        "threshold",
        this.form.ruleDetails.threshold || "1"
      );
      this.$set(
        this.form.ruleDetails,
        "rule1Type",
        this.form.ruleDetails.rule1Type || "GT"
      );
      this.$set(
        this.form.ruleDetails,
        "rule1Value",
        this.form.ruleDetails.rule1Value || 0
      );
      this.$set(
        this.form.ruleDetails,
        "rule2Type",
        this.form.ruleDetails.rule2Type || "GT"
      );
      this.$set(
        this.form.ruleDetails,
        "rule2Value",
        this.form.ruleDetails.rule2Value || 0
      );

      this.$set(
        this.form.ruleDetails,
        "referenceFields",
        this.form.ruleDetails.referenceFields || [{ name: "", value: "" }]
      );

      this.isShowDeleteTimeField =
        this.form.ruleDetails.referenceFields.length > 1 ? true : false;
      this.isShowDeleteAssociatedField1 =
        this.form.ruleDetails.alertConditions &&
        this.form.ruleDetails.alertConditions[0].associationField.length > 1
          ? true
          : false;
      this.isShowDeleteAssociatedField2 =
        this.form.ruleDetails.alertConditions &&
        this.form.ruleDetails.alertConditions[1].associationField.length > 1
          ? true
          : false;

      if (this.form.ruleDetails.condition) {
        this.form.ruleDetails.conditionStr = generateSqlString(
          generateTuple(this.form.ruleDetails.condition)
        );
      } else {
        this.$set(this.form.ruleDetails, "condition", null);
        this.$set(this.form.ruleDetails, "conditionStr", null);
      }

      if (this.form.ruleDetails.alertConditions) {
        if (this.form.ruleDetails.alertConditions[0].condition) {
          this.form.ruleDetails.alertConditions[0].conditionStr =
            generateSqlString(
              generateTuple(this.form.ruleDetails.alertConditions[0].condition)
            );
          this.form.ruleDetails.alertConditions[1].conditionStr =
            generateSqlString(
              generateTuple(this.form.ruleDetails.alertConditions[1].condition)
            );
        } else {
          this.form.ruleDetails.alertConditions[0].conditionStr = "";
          this.form.ruleDetails.alertConditions[1].conditionStr = "";
        }
      } else {
        this.$set(this.form.ruleDetails, "alertConditions", []);
        this.$set(this.form.ruleDetails.alertConditions, 0, {
          dataSourceId: null, //索引类型1
          conditionStr: "", //过滤规则1
          condition: null,
          associationField: [""], // 关联字段1
        });
        this.$set(this.form.ruleDetails.alertConditions, 1, {
          dataSourceId: null, //索引类型1
          conditionStr: "", //过滤规则1
          condition: null,
          associationField: [""], // 关联字段1
        });
      }
      this.form.degree = this.form.degree.toString();
      this.form.alertDegreeUp = this.form.alertDegreeUp.toString();
      this.changeRuleType(this.form.ruleType);
      this.getAlarmByCode(this.form.alarmCode);
    } else {
      this.optionList = this.typeStaticListOption;
      this.changeData();
      this.form.degree = this.alarmLevelList[0].id;
      // this.form.alertType = this.alertTypeList[0].paramValue;
      this.form.alertDegreeUp = this.form.alertDegreeUp.toString();
      this.form.ruleDetails.dataSourceId = this.indexTypes[0].paramValue;
      this.changeRuleType("singleAlert");
      this.getAutoRuleCode();
    }
  },

  data() {
    return {
      relationOption1: [], // 规则类别为"多数据源时序告警"时关联字段1的下拉数据
      relationOption2: [], // 规则类别为"多数据源时序告警"时关联字段2的下拉数据
      optionList: [],
      loading: false,
      isShowAlarmSelect: false,
      unitOptions: UNITOPTIONS,
      statsTypeList: STATISTICALS, //事件数量 统计方式列表
      statsTypeList2: STATISTICALS.slice(1), //基线对比 统计方式列表
      conditionOperaOptions: CONDITIONOPERATOR, //条件1\条件2符号列表
      isShowDeleteTimeField: false,
      isShowDeleteAssociatedField1: false,
      isShowDeleteAssociatedField2: false,
      form: {
        //共有类型
        ruleType: "singleAlert", //告警规则类别
        alarmCode: null, //告警码
        ruleCode: null, //规则编码
        ruleName: null, //规则名称
        degree: null, //告警等级
        alertDegreeUp: 0, //级别提升基数
        // alertType: null, //规则类型
        killChain: 5, //杀伤链
        remark: null, //备注 (AI分析 无)
        isDefault: 0, //自定义
        alarmType: "",
        alarmType: "",

        //私有
        ruleDetails: {
          ruleType: "singleAlert",
          dataSourceId: null, //索引类型   (单条、事件、单数据源、基线、多维度)
          mergeField: [], //告警合并字段 (单条、事件、单数据源、基线、多维度)
          conditionStr: null, //过滤规则        (单条、事件、单数据源、基线、多维度)
          condition: null,

          frequency: "1", //监控频率 (事件、多数据源、单数据源、基线、多维度)
          frequencyType: "min",

          timesInterval: "1", //统计时间范围 (事件、多数据源、单数据源、基线、多维度)
          intervalType: "min",

          statsType: "", //统计方式 (事件、基线)
          threshold: "1", //阈值 (事件)

          alertConditions: [
            {
              //(多数据源)
              dataSourceId: null, //索引类型1
              conditionStr: "", //过滤规则1
              condition: null,
              associationField: [""], // 关联字段1
            },
            {
              //(多数据源)
              dataSourceId: null, //索引类型2
              conditionStr: "", //过滤规则2
              condition: null,
              associationField: [""], // 关联字段2
            },
          ],

          referenceFields: [{ name: "", value: "" }], // 时序字段值(单数据源)

          limitInterval: null, //对比时间范围 (基线)
          limitField: null, //对比字段 (基线)
          aggField: null, //指标字段 (事件，除去统计方式是值统计)
          rule1Type: "GT", //条件1 (基线)
          rule1Value: 0,
          rule2Type: "GT", //条件2 (基线)
          rule2Value: 0,

          logNum: null, //日志条数 (多维度)
          dimensionDiff: null, //不同维度 (多维度)
        },
      },
      rules: {
        ruleType: [
          {
            required: true,
            message: "请选择告警规则类别",
            trigger: "blur",
          },
        ],
        alarmCode: [
          { required: true, message: "请选择告警码", trigger: "change" },
        ],
        ruleCode: [
          { required: true, message: "请输入规则编码", trigger: "blur" },
        ],
        ruleName: [
          { required: true, message: "请输入规则名称", trigger: "blur" },
        ],
        // degree: [
        //   { required: true, message: "请选择告警等级", trigger: "blur" },
        // ],
        // alertDegreeUp: [
        //   { required: true, message: "请输入级别提升基数", trigger: "blur" },
        // ],
        // alertType: [
        //   { required: true, message: "请选择规则类型", trigger: "blur" },
        // ],
        killChain: [
          { required: true, message: "请选择杀伤链", trigger: "blur" },
        ],

        //索引类型 (单条、事件、基线、多维度)
        "ruleDetails.dataSourceId": [
          { required: false, message: "请选择索引类型", trigger: "blur" },
        ],

        //过滤规则 (单条、事件、基线、多维度)
        "ruleDetails.conditionStr": [
          { required: false, message: "请配置过滤规则", trigger: "blur" },
        ],

        //监控频率 (事件、多数据源、基线、多维度)
        "ruleDetails.frequency": [
          { required: false, message: "请输入监控频率", trigger: "blur" },
        ],

        //统计时间范围 (事件、多数据源、基线、多维度)
        "ruleDetails.timesInterval": [
          { required: false, message: "请输入统计时间范围", trigger: "blur" },
        ],

        //统计方式 (事件)
        "ruleDetails.statsType": [
          { required: false, message: "请选择统计方式", trigger: "blur" },
        ],

        //阈值 (事件)
        "ruleDetails.threshold": [
          { required: false, message: "请输入阈值", trigger: "blur" },
        ],

        //时序字段值 (单数据源)
        "ruleDetails.referenceFields.0.name": [
          { required: false, message: "请选择时序字段值", trigger: "blur" },
        ],
        "ruleDetails.referenceFields.0.value": [
          { required: false, message: "请输入时序字段值", trigger: "blur" },
        ],

        //关联字段1 (多数据源)
        "ruleDetails.alertConditions[0].associationField.0": [
          { required: false, message: "请选择关联字段", trigger: "blur" },
        ],
        //关联字段2 (多数据源)
        "ruleDetails.alertConditions[1].associationField.0": [
          { required: false, message: "请选择关联字段", trigger: "blur" },
        ],

        //索引类型1 (多数据源)
        "ruleDetails.alertConditions[0].dataSourceId": [
          { required: false, message: "请选择索引类型", trigger: "blur" },
        ],
        //索引类型2 (多数据源)
        "ruleDetails.alertConditions[1].dataSourceId": [
          { required: false, message: "请选择索引类型", trigger: "blur" },
        ],

        //过滤规则1 (多数据源)
        "ruleDetails.alertConditions[0].conditionStr": [
          { required: false, message: "请选择过滤规则", trigger: "blur" },
        ],
        //过滤规则2 (多数据源)
        "ruleDetails.alertConditions[1].conditionStr": [
          { required: false, message: "请选择过滤规则", trigger: "blur" },
        ],

        //对比时间范围 (基线)
        "ruleDetails.limitInterval": [
          { required: false, message: "请输入对比时间范围", trigger: "blur" },
        ],

        //对比字段 (基线)
        "ruleDetails.limitField": [
          { required: false, message: "请选择对比字段", trigger: "blur" },
        ],
        //指标字段 (事件数量)
        "ruleDetails.aggField": [
          { required: false, message: "请选择指标字段", trigger: "blur" },
        ],

        //条件1 (基线)
        "ruleDetails.rule1Type": [
          { required: false, message: "请输入条件1", trigger: "blur" },
        ],

        // 日志条数(多维度)
        "ruleDetails.logNum": [
          { required: false, message: "请输入日志条数", trigger: "blur" },
        ],
        //不同维度(多维度)
        "ruleDetails.dimensionDiff": [
          { required: false, message: "请输入不同维度", trigger: "blur" },
        ],
      },
    };
  },

  methods: {
    // 告警合并字段处理数据
    changeData() {
      let staticData = this.optionList.filter(
        (item) => item.isStatistics === 0
      );
      //关联字段/时序字段值/不同维度/规则查询条件
      let searchData = this.optionList.filter((item) => item.isSearch === 0);
      //指标字段/对比字段
      let showData = this.optionList.filter((item) => item.isShow === 0);

      //指标字段
      let showLessData = this.optionList.filter(
        (item) =>
          item.isShow === 0 &&
          (item.columnType === "Integer" ||
            item.columnType === "Double" ||
            item.columnType === "float" ||
            item.columnType === "Long")
      );

      this.$store.commit("rule/SET_TYPE_STATIC_LIST", staticData || []);
      this.$store.commit("rule/SET_TYPE_SEARCH_LIST", searchData || []);
      this.$store.commit("rule/SET_TYPE_SHOW_LIST", showData || []);
      this.$store.commit("rule/SET_TYPE_SHOW_LESS_LIST", showLessData || []);
    },
    // 切换索引类型时，重置其他相关值
    changeIndexType(val) {
      if (this.form.ruleDetails.mergeField) {
        this.form.ruleDetails.mergeField = "";
      }
      if (this.form.ruleDetails.conditionStr) {
        this.form.ruleDetails.conditionStr = "";
      }

      if (this.form.ruleDetails.aggField) {
        this.form.ruleDetails.aggField = null;
      }

      if (this.form.ruleDetails.dimensionDiff) {
        this.form.ruleDetails.dimensionDiff = null;
      }

      if (this.form.ruleDetails.limitField) {
        this.form.ruleDetails.limitField = null;
      }
      if (this.form.ruleDetails.referenceFields[0].name) {
        this.form.ruleDetails.referenceFields[0].name = null;
      }
      this.optionList =
        val === "index-bwda-ark-apisecurity"
          ? this.typeStaticOption
          : this.typeStaticListOption;

      this.changeData();
    },
    // 多数据源时序告警 索引类型1修改时重置相关字段值
    changeTypeIndex1(val) {
      this.form.ruleDetails.alertConditions[0].associationField = [""];
      this.form.ruleDetails.alertConditions[0].conditionStr = null;
      this.form.ruleDetails.alertConditions[0].condition = null;
      let option =
        val === "index-bwda-ark-apisecurity"
          ? this.typeStaticOption
          : this.typeStaticListOption;

      this.relationOption1 = option.filter((item) => item.isSearch === 0);
    },
    // 多数据源时序告警 索引类型2修改时重置相关字段值
    changeTypeIndex2(val) {
      this.form.ruleDetails.alertConditions[1].associationField = [""];
      this.form.ruleDetails.alertConditions[1].conditionStr = null;
      this.form.ruleDetails.alertConditions[1].condition = null;
      let option =
        val === "index-bwda-ark-apisecurity"
          ? this.typeStaticOption
          : this.typeStaticListOption;
      this.relationOption2 = option.filter((item) => item.isSearch === 0);
    },
    getAutoRuleCode() {
      autoRuleCode().then((res) => {
        if (res && res.code == "200") {
          this.form.ruleCode = res.data;
        } else {
          this.form.ruleCode = "";
        }
      });
    },
    getAlarmByCode(alarmCode) {
      alarmCodeList({ pageNo: 1, pageSize: 10, alarmCode: alarmCode }).then(
        (res) => {
          if (res && res.code === 200) {
            let data = res.rows && res.rows[0];
            this.$set(
              this.form,
              "alarmType",
              this.transformAlarmType(data.alarmType)
            );
            this.$set(this.form, "alarmBusiness", data.alarmBusinessName);
            this.$set(this.form, "degree", data.alarmLevel.toString());
          }
        }
      );
    },
    transformAlarmType(val) {
      let alarmOption = this.alarmTypeList.find((item) => +item.id === val);
      return alarmOption && alarmOption.name;
    },
    transformAlarmLevel(val) {
      let alarmLevelOption = this.alarmLevelList.find(
        (item) => +item.id === val
      );
      return alarmLevelOption && alarmLevelOption.name;
    },

    getAlarmCode(param) {
      this.isShowAlarmSelect = false;
      this.form.alarmCode = param.alarmCode;
      this.form.alarmBusiness = param.alarmBusinessName;
      this.form.alarmType = param.alarmTypeCN;
      this.form.degree = param.alarmLevel.toString();
    },
    toAlarmList() {
      this.isShowAlarmSelect = true;
    },
    //时序字段值 新增
    addTimeField(item) {
      this.form.ruleDetails.referenceFields.push({
        name: this.form.ruleDetails.referenceFields[0].name,
        value: "",
      });
      this.isShowDeleteTimeField =
        this.form.ruleDetails.referenceFields.length > 1 ? true : false;

      // const index = this.form.ruleDetails.referenceFields.length - 1;
      // this.$set(this.rules, `ruleDetails.referenceFields.${index}.name`, [
      //   {
      //     type: "string",
      //     required: true,
      //     message: "请选择时序字段值",
      //     trigger: "blur",
      //   },
      // ]);
      // this.$set(this.rules, `ruleDetails.referenceFields.${index}.value`, [
      //   {
      //     type: "string",
      //     required: true,
      //     message: "请输入时序字段值",
      //     trigger: "blur",
      //   },
      // ]);
    },

    //时序字段值 删除
    deleteTimeField(item, index) {
      this.form.ruleDetails.referenceFields.splice(index, 1);
      this.isShowDeleteTimeField =
        this.form.ruleDetails.referenceFields.length > 1 ? true : false;

      const index2 = this.form.ruleDetails.referenceFields.length;
      delete this.rules[`ruleDetails.referenceFields.${index2}.name`];
      delete this.rules[`ruleDetails.referenceFields.${index2}.value`];
    },

    //关联字段1 新增
    addAssociatedField1(item) {
      this.form.ruleDetails.alertConditions[0].associationField.push("");
      this.isShowDeleteAssociatedField1 =
        this.form.ruleDetails.alertConditions[0].associationField.length > 1
          ? true
          : false;

      const index =
        this.form.ruleDetails.alertConditions[0].associationField.length - 1;
      this.$set(
        this.rules,
        `ruleDetails.alertConditions[0].associationField.${index}`,
        [
          {
            type: "string",
            required: true,
            message: "请选择关联字段",
            trigger: "blur",
          },
        ]
      );
    },

    //关联字段1 删除
    deleteAssociatedField1(item, index) {
      this.form.ruleDetails.alertConditions[0].associationField.splice(
        index,
        1
      );
      this.isShowDeleteAssociatedField1 =
        this.form.ruleDetails.alertConditions[0].associationField.length > 1
          ? true
          : false;

      const index2 =
        this.form.ruleDetails.alertConditions[0].associationField.length;
      delete this.rules[
        `ruleDetails.alertConditions[0].associationField.${index2}`
      ];
    },

    //关联字段2 新增
    addAssociatedField2(item) {
      this.form.ruleDetails.alertConditions[1].associationField.push("");
      this.isShowDeleteAssociatedField2 =
        this.form.ruleDetails.alertConditions[1].associationField.length > 1
          ? true
          : false;

      const index =
        this.form.ruleDetails.alertConditions[1].associationField.length - 1;
      this.$set(
        this.rules,
        `ruleDetails.alertConditions[1].associationField.${index}`,
        [
          {
            type: "string",
            required: true,
            message: "请选择时序字段值",
            trigger: "blur",
          },
        ]
      );
    },

    //关联字段2 删除
    deleteAssociatedField2(item, index) {
      this.form.ruleDetails.alertConditions[1].associationField.splice(
        index,
        1
      );
      this.isShowDeleteAssociatedField2 =
        this.form.ruleDetails.alertConditions[1].associationField.length > 1
          ? true
          : false;

      const index2 =
        this.form.ruleDetails.alertConditions[1].associationField.length;
      delete this.rules[
        `ruleDetails.alertConditions[1].associationField.${index2}`
      ];
    },

    changeRuleType(val) {
      this.optionList =
        this.form.ruleDetails.dataSourceId === "index-bwda-ark-apisecurity"
          ? this.typeStaticOption
          : this.typeStaticListOption;

      this.changeData();
      if (val === "countAlert") {
        //事件
        this.rules["ruleDetails.statsType"][0].required = true; //统计方式
        this.rules["ruleDetails.threshold"][0].required = true; //阈值

        if (this.form.ruleDetails.statsType !== "count") {
          this.rules["ruleDetails.aggField"][0].required = true; //指标字段
        } else {
          this.rules["ruleDetails.aggField"][0].required = false;
        }
      } else if (val === "multiSequenceAlert") {
        //   多数据源时序告警
        this.rules[
          "ruleDetails.alertConditions[0].associationField.0"
        ][0].required = true; //关联字段1
        this.rules[
          "ruleDetails.alertConditions[1].associationField.0"
        ][0].required = true; //关联字段2

        this.rules[
          "ruleDetails.alertConditions[0].dataSourceId"
        ][0].required = true; //索引类型1
        this.rules[
          "ruleDetails.alertConditions[1].dataSourceId"
        ][0].required = true; //索引类型2
        this.rules[
          "ruleDetails.alertConditions[0].conditionStr"
        ][0].required = true; //过滤条件1
        this.rules[
          "ruleDetails.alertConditions[1].conditionStr"
        ][0].required = true; //过滤条件2
      } else if (val === "singleSequenceAlert") {
        //   单数据源时序告警
        this.rules["ruleDetails.referenceFields.0.name"][0].required = true; //时序字段值 name
        this.rules["ruleDetails.referenceFields.0.value"][0].required = true; //时序字段值 value
      } else if (val === "limitAlert") {
        //   基线对比告警
        this.rules["ruleDetails.limitInterval"][0].required = true; //对比时间范围
        this.rules["ruleDetails.statsType"][0].required = true; //统计方式
        this.rules["ruleDetails.limitField"][0].required = true; //对比字段
        this.rules["ruleDetails.rule1Type"][0].required = true; //条件1
      } else if (val === "multiAlert") {
        //   多维度关联告警
        this.rules["ruleDetails.logNum"][0].required = true; //日志条数
        this.rules["ruleDetails.dimensionDiff"][0].required = true; //不同维度
      }

      //多数据源时序告警  AI分析告警
      if (val === "multiSequenceAlert" || val === "analyseAlert") {
        this.rules["ruleDetails.dataSourceId"][0].required = false; //索引类型
        this.rules["ruleDetails.conditionStr"][0].required = false; //过滤规则
      } else {
        this.rules["ruleDetails.dataSourceId"][0].required = true; //索引类型
        this.rules["ruleDetails.conditionStr"][0].required = true; //过滤规则
      }

      //单条日志告警  AI分析告警
      if (val === "singleAlert" || val === "analyseAlert") {
        this.rules["ruleDetails.frequency"][0].required = false; //监控频率
        this.rules["ruleDetails.timesInterval"][0].required = false; //统计时间范围
      } else {
        this.rules["ruleDetails.frequency"][0].required = true; //监控频率
        this.rules["ruleDetails.timesInterval"][0].required = true; //统计时间范围
      }
    },

    //过滤规则配置页展示
    toConfigure(param) {
      if (this.form.ruleType === "multiSequenceAlert") {
        // 当告警规则类别为多数据源时序告警时，点击各个配置按钮，根据关联字段里面的不同数据修改vuex里面的SET_TYPE_SEARCH_LIST的值
        if (param === 1) {
          this.$store.commit(
            "rule/SET_TYPE_SEARCH_LIST",
            this.relationOption1 || []
          );
          this.$refs.configure.show(
            param,
            this.form.ruleDetails.alertConditions[0].condition
          );
        } else {
          this.$store.commit(
            "rule/SET_TYPE_SEARCH_LIST",
            this.relationOption2 || []
          );
          this.$refs.configure.show(
            param,
            this.form.ruleDetails.alertConditions[1].condition
          );
        }
      } else {
        if (this.form.ruleDetails.conditionStr) {
          this.$refs.configure.show(param, this.form.ruleDetails.condition);
        } else {
          this.$refs.configure.show(param);
        }
      }
    },

    //获取过滤规则
    getCondition(conditionStr, conditionObj, currentWhich) {
      if (+currentWhich === 1) {
        this.form.ruleDetails.alertConditions[0].conditionStr = conditionStr;
        this.form.ruleDetails.alertConditions[0].condition = conditionObj;
      } else if (+currentWhich === 2) {
        this.form.ruleDetails.alertConditions[1].conditionStr = conditionStr;
        this.form.ruleDetails.alertConditions[1].condition = conditionObj;
      } else {
        this.form.ruleDetails.conditionStr = conditionStr;
        this.form.ruleDetails.condition = conditionObj;
      }
      this.$forceUpdate();
    },

    //保存
    submit() {
      // console.log(this.form);
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.form.ruleDetails.hasOwnProperty("conditionStr")) {
            delete this.form.ruleDetails.conditionStr;
          }

          if (
            this.form.ruleType === "multiSequenceAlert" &&
            this.form.ruleDetails.alertConditions[0].associationField.length !==
              this.form.ruleDetails.alertConditions[1].associationField.length
          ) {
            this.$GlobalErrorConfirm(
              "保存失败",
              "关联字段1和关联字段2数量需相同"
            );
            return;
          }

          if (this.form.ruleType === "singleSequenceAlert") {
            this.form.ruleDetails.referenceFields =
              this.form.ruleDetails.referenceFields.filter((item) => {
                return item.value !== "";
              });

            if (this.form.ruleDetails.referenceFields.length > 1) {
              let result = this.form.ruleDetails.referenceFields
                .slice(1)
                .some((item) => {
                  return (
                    item.value ===
                    this.form.ruleDetails.referenceFields[0].value
                  );
                });

              if (result) {
                this.$GlobalErrorConfirm("保存失败", "时序字段值存在相同的值");
                return;
              }

              this.form.ruleDetails.referenceFields.forEach((item) => {
                item.name = this.form.ruleDetails.referenceFields[0].name;
              });
            }
          }
          this.loading = true;

          let params = {
            ruleTypeId: this.form.killChain,
            ruleType: this.form.ruleType, //告警规则类别
            alarmCode: this.form.alarmCode, //告警码
            ruleCode: this.form.ruleCode, //规则编码
            ruleName: this.form.ruleName, //规则名称
            degree: +this.form.degree, //告警等级
            alertDegreeUp: this.form.alertDegreeUp, //级别提升基数
            // alertType: this.form.alertType, //规则类型
            killChain: this.form.killChain, //杀伤链
            alarmType: this.form.alarmType, //告警类型
            alarmBusiness: this.form.alarmBusiness, //告警子类型
            ruleDetails: {},
          };

          params = getAlarmRulesParams(params, this.form);

          params.ruleDetails = JSON.stringify(params.ruleDetails);
          if (this.isEdit) {
            params.alertRuleId = this.form.alertRuleId;
            params.status = this.form.status;
            ruleEdit(params)
              .then((res) => {
                if (res && res.code === 200) {
                  this.$emit("close", params);
                  this.$GlobalSuccessMessage("编辑成功");
                } else {
                  this.$GlobalErrorConfirm("编辑失败", res.msg);
                }
              })
              .finally(() => {
                this.loading = false;
              });
          } else {
            params.isDefault = 0;
            ruleAdd(params)
              .then((res) => {
                if (res && res.code === 200) {
                  this.$emit("close", params);
                  this.$GlobalSuccessMessage("新增成功");
                } else {
                  this.$GlobalErrorConfirm("新增失败", res.msg);
                }
              })
              .finally(() => {
                this.loading = false;
              });
          }
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
@import "./index.scss";
</style>
